package com.goldgov.code.template.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.goldgov.code.meta.FieldInfo;
import com.goldgov.code.meta.ModuleInfo;
import com.goldgov.code.meta.RelationInfo;
import com.goldgov.code.template.Group;
import com.goldgov.code.template.Layout;
import com.goldgov.code.template.ParamKeyName;
import com.goldgov.code.template.point.CodePoint;
import com.goldgov.code.template.point.PointMapping;
import com.goldgov.code.template.point.impl.ImportCodePoint;
import com.goldgov.code.utils.PackageUtils;

public class BeanLayout implements Layout{

	public static final String PACKAGE_NAME = "service";
	
	@Override
	public String packageName() {
		return PACKAGE_NAME;
	}

	@Override
	public String templatePath() {
		return "bean";
	}

	@Override
	public Class<?>[] dependOn() {
		return null;
	}

	@Override
	public PointMapping codePoint(String packageName,ModuleInfo moduleInfo,Group group,Map<String,Object> paramMap) {
		
		PointMapping pointMapping = new PointMapping();
		List<CodePoint> codePointList = pointMapping.codePoint("*/__name__.java.ftl");
		List<CodePoint> codePointQueryList = pointMapping.codePoint("*/__name__Query.java.ftl");
		
		RelationInfo relation = moduleInfo.getRelation();
		if(relation != null) {
			ModuleInfo relationModule = relation.getRelationModule();
			codePointList.add(new ImportCodePoint(PackageUtils.joinPackageName(packageName,relationModule.getPackageName(),BeanLayout.PACKAGE_NAME,relationModule.getName())));
		}
		
		//判断是否要导出默认字段，如果导出则不需要处理Data类型，因为在默认字段中已经处理了。
		Object genDefaultField = ((Map<String,Object>)paramMap.get("extParam")).get(ParamKeyName.GEN_DEFAULT_FIELD);
		boolean hasDefField = genDefaultField != null && Boolean.valueOf(genDefaultField.toString());
		FieldInfo[] fields = moduleInfo.getFields();
		boolean moduleImpDate = false;
		boolean queryImpDate = false;
		for (FieldInfo fieldInfo : fields) {
			if(fieldInfo.getType().getJavaType() == Date.class && !hasDefField && !moduleImpDate) {
				codePointList.add(new ImportCodePoint(Date.class));
				moduleImpDate = true;
			}
			if(fieldInfo.getConstraint() != null && fieldInfo.getConstraint().getQueryType() != null && !queryImpDate) {
				codePointQueryList.add(new ImportCodePoint(Date.class));
				queryImpDate = true;
			}
		}
		
		return pointMapping;
	}

	@Override
	public void init(ModuleInfo moduleInfo) {
		
	}

}
